package com.cn.customer.housekeeping.housekeepingc.ui.fragment;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.cn.customer.housekeeping.housekeepingc.R;
import com.cn.customer.housekeeping.housekeepingc.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingc.bean.ServicedetailResp;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingc.biz.personcenter.InterfacePresenter;
import com.cn.customer.housekeeping.housekeepingc.constant.Constants;
import com.cn.customer.housekeeping.housekeepingc.constant.URLUtil;
import com.cn.customer.housekeeping.housekeepingc.ui.OrderDetailsActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.adapter.NetworkImageHolderView;
import com.cn.customer.housekeeping.housekeepingc.ui.base.BaseImgView;
import com.cn.customer.housekeeping.housekeepingc.util.NavUtils;
import com.cn.customer.housekeeping.housekeepingc.view.SlideDetailsLayout;
import com.google.gson.Gson;
import com.gxz.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;



/**
 * 商品详情中===服务
 */
public class OrderServiceFragment extends Fragment implements View.OnClickListener, SlideDetailsLayout.OnSlideDetailsListener, IUserLoginView {
    private InterfacePresenter mInterfacePresenter;
    private PagerSlidingTabStrip psts_tabs;
    private SlideDetailsLayout sv_switch;
    private ScrollView sv_goods_info;
    public ConvenientBanner vp_item_goods_img;
    private LinearLayout ll_goods_detail, ll_goods_config;
    private TextView tv_goods_detail, tv_goods_config;
    private View v_tab_cursor;
    public FrameLayout fl_content;
    public LinearLayout ll_comment, ll_pull_up, ll_goumai,ll_shangpu;
    public TextView tv_haoping, tv_dinggou, tv_goods_title, tv_new_price, tv_old_price, tv_comment_count, tv_weidai, tv_xiaoshounum, tv_fuwushijian, tv_fuwumiaoshu, tv_shangjianame, tv_jiedan;
    public ImageView iv_ensure;

    public GoodsDetailWebFragment goodsInfoWebFragment;
    private List<TextView> tabTextList;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    public OrderDetailsActivity activity;
    private LayoutInflater inflater;
    private String serviceid;
    private ServicedetailResp mServicedetailResp;

    public static OrderServiceFragment newInstance(String serviceid) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.serviceid, serviceid);
        OrderServiceFragment fragment = new OrderServiceFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (OrderDetailsActivity) context;
    }

    @Override
    public void onResume() {
        super.onResume();
        vp_item_goods_img.startTurning(4000);
    }

    @Override
    public void onPause() {
        super.onPause();
        vp_item_goods_img.stopTurning();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        View rootView = inflater.inflate(R.layout.fragment_orderservice, null);
        mInterfacePresenter = new InterfacePresenter();
        mInterfacePresenter.attachView(this);
        initView(rootView);
        initListener();
        initData();
        mInterfacePresenter.getServicedetail(serviceid);
        return rootView;
    }

    private void initListener() {
        ll_goumai.setOnClickListener(this);
        ll_comment.setOnClickListener(this);
        ll_pull_up.setOnClickListener(this);
        ll_goods_detail.setOnClickListener(this);
        ll_goods_config.setOnClickListener(this);
        sv_switch.setOnSlideDetailsListener(this);
        ll_shangpu.setOnClickListener(this);
    }


    private void initView(View rootView) {
        ll_goumai = (LinearLayout) rootView.findViewById(R.id.ll_goumai);
        psts_tabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.psts_tabs);
        sv_switch = (SlideDetailsLayout) rootView.findViewById(R.id.sv_switch);
        sv_goods_info = (ScrollView) rootView.findViewById(R.id.sv_goods_info);
        v_tab_cursor = rootView.findViewById(R.id.v_tab_cursor);
        vp_item_goods_img = (ConvenientBanner) rootView.findViewById(R.id.vp_item_goods_img);
        fl_content = (FrameLayout) rootView.findViewById(R.id.fl_content);
        ll_comment = (LinearLayout) rootView.findViewById(R.id.ll_comment);
        ll_pull_up = (LinearLayout) rootView.findViewById(R.id.ll_pull_up);
        ll_goods_detail = (LinearLayout) rootView.findViewById(R.id.ll_goods_detail);
        ll_goods_config = (LinearLayout) rootView.findViewById(R.id.ll_goods_config);
        tv_goods_detail = (TextView) rootView.findViewById(R.id.tv_goods_detail);
        tv_goods_config = (TextView) rootView.findViewById(R.id.tv_goods_config);
        tv_goods_title = (TextView) rootView.findViewById(R.id.tv_goods_title);
        tv_new_price = (TextView) rootView.findViewById(R.id.tv_new_price);
        tv_old_price = (TextView) rootView.findViewById(R.id.tv_old_price);
        tv_weidai = (TextView) rootView.findViewById(R.id.tv_weidai);
        tv_xiaoshounum = (TextView) rootView.findViewById(R.id.tv_xiaoshounum);
        tv_fuwushijian = (TextView) rootView.findViewById(R.id.tv_fuwushijian);
        iv_ensure = (ImageView) rootView.findViewById(R.id.iv_ensure);
        tv_fuwumiaoshu = (TextView) rootView.findViewById(R.id.tv_fuwumiaoshu);
        tv_comment_count = (TextView) rootView.findViewById(R.id.tv_comment_count);
        tv_shangjianame = (TextView) rootView.findViewById(R.id.tv_shangjianame);
        tv_jiedan = (TextView) rootView.findViewById(R.id.tv_jiedan);
        tv_dinggou = (TextView) rootView.findViewById(R.id.tv_dinggou);
        tv_haoping = (TextView) rootView.findViewById(R.id.tv_haoping);
        ll_shangpu = (LinearLayout) rootView.findViewById(R.id.ll_shangpu);
        setDetailData();
//        setLoopView();
        tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        vp_item_goods_img.setPageIndicator(new int[]{R.mipmap.index_category_indicator_nor, R.mipmap.index_category_indicator_selected});
        vp_item_goods_img.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }


    private void initData() {
        serviceid = getArguments().getString(Constants.serviceid);
        fragmentList = new ArrayList<>();
        tabTextList = new ArrayList<>();
        tabTextList.add(tv_goods_detail);
        tabTextList.add(tv_goods_config);

    }

    /**
     * 加载完商品详情执行
     */
    public void setDetailData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_pull_up:
                //上拉查看图文详情
                sv_switch.smoothOpen(true);
                break;
            case R.id.ll_goumai://购买
                NavUtils.gotoMerchandisePlaceOrderActivity(getActivity(),serviceid);
                break;
            case R.id.ll_shangpu://店铺
                NavUtils.gotoShopOrderListActivity    (getActivity(), mServicedetailResp.getShopid());
                break;
            default:
                break;
        }
    }


    @Override
    public void onStatucChanged(SlideDetailsLayout.Status status) {
        if (status == SlideDetailsLayout.Status.OPEN) {
            activity.vp_content.setNoScroll(true);
            activity.tv_title.setVisibility(View.VISIBLE);
            activity.psts_tabs.setVisibility(View.GONE);
        } else {
            activity.vp_content.setNoScroll(false);
            activity.tv_title.setVisibility(View.GONE);
            activity.psts_tabs.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onError(BaseResp s) {

    }

    @Override
    public void onSuccess(BaseResp s) {
        if (s.getUrl().equals(URLUtil.url_get_servicedetail)) {//
            Gson gson = new Gson();
            mServicedetailResp = gson.fromJson(s.getData(), ServicedetailResp.class);
            setData(mServicedetailResp);
        }
    }

    public void setData(ServicedetailResp mServicedetailResp) {
        //初始化商品图片轮播
        vp_item_goods_img.setPages(new CBViewHolderCreator() {
            @Override
            public Object createHolder() {
                return new NetworkImageHolderView();
            }
        }, mServicedetailResp.getImgurl());


        //商品名称
        tv_goods_title.setText(mServicedetailResp.getServicename());
        //商品价格
        tv_new_price.setText(mServicedetailResp.getPrice());
        //商品单位
        tv_weidai.setText("元/" + mServicedetailResp.getUnit());
        //商品原价
        tv_old_price.setText("￥" + mServicedetailResp.getOriginalprice());
        //销售数量
        tv_xiaoshounum.setText("已售 " + mServicedetailResp.getSaledcount()+" 单");
        //保障safeguard
        BaseImgView.ImageLoaderLoadimg(iv_ensure,
                mServicedetailResp.getSafeguard(), R.mipmap.icon_fang, R.mipmap.icon_fang, R.mipmap.icon_fang, 60, 60, 20);
        //服务时间
        tv_fuwushijian.setText(mServicedetailResp.getServicetime());
        //服务描述
        tv_fuwumiaoshu.setText(mServicedetailResp.getServicedesc());
        //服务描述
        tv_shangjianame.setText(mServicedetailResp.getServiceshop());
        //接单
        tv_jiedan.setText("接单率 " + mServicedetailResp.getAcceptorderrate()+"%");
        //好评
        tv_haoping.setText("好评率 " + mServicedetailResp.getWellcommentrate()+"%");
        //订购须知
        tv_dinggou.setText(mServicedetailResp.getOrderinfo());

//        网页
        goodsInfoWebFragment = new GoodsDetailWebFragment().newInstance(mServicedetailResp.getOrderinfoweburl());
        fragmentList.add(goodsInfoWebFragment);
//        nowFragment = goodsInfoWebFragment;
        fragmentManager = getChildFragmentManager();
        //默认显示商品详情tab
        fragmentManager.beginTransaction().replace(R.id.fl_content, goodsInfoWebFragment).commitAllowingStateLoss();

        //
        ((OrderDetailsActivity) getActivity()).mGoodsDetailWebFragment.wv_detail.loadUrl(mServicedetailResp.getOrderinfoweburl());

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
